<?php namespace app\admin\controller;
class Article extends Common
{
   protected $db;

   public function __init()
   {
	  $this -> db = new \system\model\Article();
   }

   //展示
   public function index()
   {
	  //调用行数，获取数据和分页信息
	  $data = $this -> db -> getAllData(2);
	  View ::with('data', $data);
	  return view();
   }

   //添加
   public function store()
   {
//       p($_POST);
	  if (IS_POST) {
		 $res = $this -> db -> store();
		 if ($res['valid']) {
			message($res['message'], u('index'), 'success');
		 }
		 message($res['message'], 'back', 'error');
	  }
	  //1.获取所属分类数据
	  $cateData = $this -> db -> getCateData();
//	   p($cateData);
	  View ::with('cateData', $cateData);
	  //2.获取所有的标签数据
	  $tagData = $this -> db -> getTagData();
//	   p($tagData);
	  View ::with('tagData', $tagData);
	  return view();
   }

   //编辑
   public function edit()
   {
	  $aid = q('get.aid');
//       p($aid);
	  if (IS_POST) {
		 $res = $this -> db -> edit($aid);
		 if ($res['valid']) {
			message($res['message'], u('index'), 'success');
		 }
		 message($res['message'], 'back', 'error');
	  }
	  //1.获取所属分类数据
	  $cateData = $this -> db -> getCateData();
//	   p($cateData);
	  View ::with('cateData', $cateData);
	  //2.获取所有的标签数据
	  $tagData = $this -> db -> getTagData();
//	   p($tagData);
	  View ::with('tagData', $tagData);
	  //3.获取旧数据
	  $oldData = $this -> db -> getOldData($aid);
//	  p($oldData);
	  View ::with('oldData', $oldData);
	  //4.获取旧标签的数据
	  $oldTag = Db ::table('article_tag') -> where('article_aid', $aid) -> lists('tag_tid');
	  View ::with('oldTag', $oldTag);
	  return view();
   }

   //回收站首页
   public function removeindex()
   {
	  $data = $this -> db -> getAllData(2, 1);
	  View ::with('data', $data);
	  return view();
   }

   //删除进回收站
   public function delfirst()
   {
	  $aid = q('get.aid');
	  $this -> db -> where('aid', $aid) -> update(['is_recycle' => 1]);
	  message('成功删除到回收站', u('index'), 'success');
   }

   //恢复数据
   public function recover()
   {
	  $aid = q('get.aid');
	  $this -> db -> where('aid', $aid) -> update(['is_recycle' => 0]);
	  message('成功恢复数据', u('removeindex'), 'success');
   }

   //彻底删除
   public function delall()
   {
	  $aid = q('get.aid');
	  if ($this -> db -> delall($aid)) {
		 message('删除成功', u('removeindex'), 'success');
	  }
	  message('删除失败', 'back', 'error');
   }

   //异步上传处理
   public function upload()
   {
	  $files = File ::upload();
	  $path = $files[0]['path'];
	  echo $path;
	  exit;
   }

   //异步删除处理
   public function delImg()
   {
	  if (IS_AJAX) {
		 $path = $_POST['path'];
		 //删除文件
		 if (file_exists($path)) {
			unlink($path);
		 }
		 echo 1;
		 exit;
	  }
   }
}
